package com.xoa.plugin.workflow.workflowservice.imlp.lnsfplugin;

import com.xoa.dev.efficiency.model.FlowAbilityMonitor;
import com.xoa.model.workflow.FlowFormType;
import com.xoa.model.workflow.FlowTypeModel;
import com.xoa.plugin.workflow.workflowservice.WorkFlowService;
import com.xoa.util.ToJson;
import org.jsoup.nodes.Document;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class Major_4 implements WorkFlowService {
    @Override
    public ToJson run(HttpServletRequest request) {
        String runId=request.getParameter("runId");
        String flowIds=request.getParameter("flowId");
        //获取转交的步骤，可能会有多个步骤并发情况1，2，3
        String nextWorks=request.getParameter("flowPrcs");
        if(nextWorks==null||nextWorks.equals("")||nextWorks.equals("0")){
            //结束此子流程
            FlowAbilityMonitor flowAbilityMonitor=new FlowAbilityMonitor();
            flowAbilityMonitor.setRunId(Integer.parseInt(runId));
            Map<String,String> map=LnsfUtil.init().getIds(LnsfUtil.init().getWorkNameValue(flowIds,runId,"责任人"));
            flowAbilityMonitor.setUserId(map.get("USER_ID"));
            flowAbilityMonitor.setUserType(3);
            flowAbilityMonitor.setWorkEnd(LnsfUtil.init().getSdf().format(new Date()));
            LnsfUtil.init().setData(flowAbilityMonitor);
        }
        //返回上一步骤具体办事人办结时间置零
        else{
            List<String> userIdList=LnsfUtil.init().getFlowAbilityMonitorMapper().getUserIdlist(runId);
            if(userIdList!=null){
                for(int i=0;i<userIdList.size();i++){
                    FlowAbilityMonitor flowAbilityMonitor=new FlowAbilityMonitor();
                    flowAbilityMonitor.setRunId(Integer.parseInt(runId));
                    flowAbilityMonitor.setUserId(userIdList.get(i));
                    flowAbilityMonitor.setUserType(5);
                    flowAbilityMonitor.setWorkEnd("");
                    LnsfUtil.init().setData(flowAbilityMonitor);
                }
            }
        }

        //查看是否所有子流程都已办结，如果都办结则开始牵头单位的工作
        Integer leadBeginFlag=1;
        List<Date> workEndList=LnsfUtil.init().getFlowAbilityMonitorMapper().getWorkEnd(runId);
        if(workEndList!=null){
            for(int i=0;i<workEndList.size();i++){
                String workEnd=LnsfUtil.init().getSdf().format(workEndList.get(i));
                if(workEnd==null||workEnd.equals("") ||workEnd.equals("2000-01-01 00:00:00")){
                    leadBeginFlag=0;
                    break;
                }
            }
            if(leadBeginFlag==1){
                /*开始牵头工作*/
                //获取当前runId 的父runId
                Map<String,Integer> runIdFlowId=LnsfUtil.init().getFlowAbilityMonitorMapper().getParentRunByRunId(runId);
                Integer parentRunId=runIdFlowId.get("RUN_ID");
                Integer flowId=runIdFlowId.get("FLOW_ID");
                String tableName = "flow_data_" + flowId;
                //得到表单值
                Map<String,String> map=LnsfUtil.init().getFlowAbilityMonitorMapper().getFlowDataMap(parentRunId+"", tableName);
                if(parentRunId!=null&&!parentRunId.equals("")){
                    FlowAbilityMonitor flowAbilityMonitor=new FlowAbilityMonitor();
                    flowAbilityMonitor.setRunId(parentRunId);
                    flowAbilityMonitor.setWorkType(1);
                    flowAbilityMonitor.setUserType(4);
                    flowAbilityMonitor.setDeptType(4);
                    flowAbilityMonitor.setWorkStart(LnsfUtil.init().getSdf().format(new Date()));
                    List<String[]> yijian= LnsfUtil.init().getdubanyijian(LnsfUtil.init().getWorkNameValue(String.valueOf(flowId),String.valueOf(parentRunId),"办公室督办意见"));
                    String[] yijian1=yijian.get(0);
                    Map<String,String> userIdAndDeptId=LnsfUtil.init().getIds(yijian1[2]);
                    flowAbilityMonitor.setUserId(userIdAndDeptId.get("USER_ID"));
                    flowAbilityMonitor.setDeptId(userIdAndDeptId.get("DEPT_ID"));
                    flowAbilityMonitor.setWorkDeadline(yijian1[3]);
                    flowAbilityMonitor.setWorkStart(LnsfUtil.init().getSdf().format(new Date()));
                    flowAbilityMonitor.setWorkTitle("牵头单位总结汇报");
                    LnsfUtil.init().setData(flowAbilityMonitor);
                }
            }
        }

        return null;
    }
}
